{% extends '_base_list.html' %}

{% block title %}
    作业中心
{% endblock %}


{% block top_css %}
    {{ super() }}
    <!--bees-->
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/bees.css') }}">
{% endblock %}


<!-- Content head -->
{% block head_title %}
    作业中心
{% endblock %}
{% block head_nav %}
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-home"></i></a></li>
        <li><a href="#">定时任务</a></li>
        <li class="active">任务管理</li>
    </ol>
{% endblock %}


<!-- Content body -->
{% block title_filter %}
    <div class="filter">
        <div class="row">
            <div class="col-xs-12 col-sm-2">
                <div class="form-group">
                    <input type="text" class="form-control" name="create_time" placeholder="请选择创建时间" autocomplete="off">
                </div>
            </div>

            <div class="col-xs-12 col-sm-3 pull-right">
                <div class="form-group">
                    <div class="input-group">
                        <input type="text" name="search" placeholder="请输入任务名称" class="form-control">
                        <span class="input-group-btn">
                            <button name="search" class="btn btn-mint" type="button">
                                <i class="fa fa-search"></i>
                            </button>
                        </span>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block body_operator %}
    <div class="operator mar-btm">
        <div class="row">
            <div class="col-xs-12 text-center">
                {% if current_user.has_perm('tasks-task-create') %}
                    <button class="btn btn-primary btn-sm" id="add"><i class=""></i> 新增任务</button>
                {% endif %}
                {% if current_user.has_perm('tasks-task-update') %}
                    <button class="btn btn-mint btn-sm" id="edit"><i class=""></i> 修改任务</button>
                {% endif %}
                {% if current_user.has_perm('tasks-task-delete') %}
                    <button class="btn btn-danger btn-sm" id="del"><i class=""></i> 删除任务</button>
                {% endif %}
                {% if current_user.has_perm('tasks-task-execute') %}
                    <button class="btn btn-purple btn-sm" id="exec"><i class=""></i> 执行任务</button>
                {% endif %}
            </div>
        </div>
    </div>
{% endblock %}

{% block body_table %}
   <table id="taskTable" class="table table-auto">
   </table>
{% endblock %}


{% block buttom_js %}
    {{ super() }}
    <!--bees-->
    <script src="{{ url_for('static', filename='js/bees.js') }}"></script>

    <script type="text/javascript">

        $(function () {
            bees.initDaterangepicker($('input[name="create_time"]'),{
                opens: "right",
                timePicker: false,
                timePickerSeconds: false,
                singleDatePicker: false,
                locale: {
                    format: 'YYYY-MM-DD'
                }
            });

            // 回填和初始化默认过滤参数
            var createTimeEle = $("input[name='create_time']");
            var searchEle = $('input[name="search"]');

            var filterParams = JSON.parse(window.sessionStorage.getItem("filterParams"));
            if(filterParams){
                // 回填表单过滤条件
                createTimeEle.val(filterParams.create_time);
                searchEle.val(filterParams.search);
                window.sessionStorage.removeItem("filterParams");
            }else {
                // 默认表单过滤条件
                filterParams = {
                    'create_time': createTimeEle.val(),
                    'search': searchEle.val()
                };
            }
            $.fn.beesTable.defaultFilterParams(filterParams);

            // 初始化表格
            var $tableEle = $("#taskTable");
            var options = {
                columns: [
                    {title: '', field: 'selected', radio: true},
                    {title: '任务名称', field: 'name', sortable: false},
                    {title: '任务函数', field: 'function', sortable: false},
                    {title: '定时调度', field: 'crontab', sortable: false},
                    {title: '间隔调度', field: 'interval', sortable: false},
                    {title: '位置参数', field: 'args', sortable: false},
                    {title: '关键字参数', field: 'kwargs', sortable: false},
                    {title: '是否启用', field: 'enabled', sortable: false,
                        formatter: function (value, row, index, field) {
                            if(value){
                                return '<span class="text text-success text-bold">启用</span>'
                            }else{
                                return '<span class="text text-danger text-bold">禁用</span>'
                            }
                        }
                    },
                    {title: '存活一个', field: 'only_once', sortable: false,
                        formatter: function (value, row, index, field) {
                            if(value){
                                return '<span class="text text-success text-bold">启用</span>'
                            }else{
                                return '<span class="text text-danger text-bold">禁用</span>'
                            }
                        }
                    },
                    {title: '描述信息', field: 'description', sortable: false},
                    {title: '创建人', field: 'create_by'},
                    {title: '创建时间', field: 'create_time', sortable: true}
                ],
                url:'{{ url_for("tasks.api-task-list") }}',
                sortName: 'create_time',
                sortOrder: 'desc'
            };
            var table = $tableEle.beesTable(options);

            // 搜索
            bees.bindTableSearch({
                table: $tableEle,
                searches: [
                    {eventType:'change', eventSource:createTimeEle, data:function () {
                            return {'create_time': createTimeEle.val()};
                        }
                    },
                    {eventType:'click', eventSource:$('button[name="search"]'), data: function () {
                            return {'search': searchEle.val()}
                        }
                    }
                ]
            });

            // 操作
            bees.bindTableOperator({
                table: $tableEle,
                operator: {
                    add: {
                        href: '{{ url_for('tasks.task-create') }}'
                    },
                    show: {
                        disable: true
                    },
                    edit: {
                        href: '{{ url_for('tasks.task-update', pk=default_pk) }}'
                    },
                    del: {
                        confirmCallback: function (data) {
                            setAjaxCSRFToken();
                            $.ajax({
                                type: 'post',
                                url: '{{ url_for("tasks.task-delete", pk=default_pk) }}'.replace(bees.defaultPK, data.id),
                                data : {},
                                success: function (result) {
                                    if (result.code != 200) {
                                        bees.tipError({
                                            message: result.data.message
                                        });
                                    }else {
                                       bees.tipSuccess({
                                            message: "删除成功"
                                        });
                                    }

                                    //reload
                                    table.bt.bootstrapTable('refresh');
                                }
                            })
                        }
                    }
                }
            });

            // 执行任务
            $('#exec').on('click', function () {
                var rows = $tableEle.bootstrapTable('getSelections');
                var data = rows[0];
                var count = rows.length;
                if(!count){
                    bees.tipWaring({
                        message: '请选择需要执行的任务'
                    });
                    return
                }

                // 显示执行确定对话框
                bees.confirmBox({
                    title: '执行确认',
                    content: '您确定要执行选中的任务吗？',
                    confirmCallback: function () {
                        setAjaxCSRFToken();
                        $.ajax({
                            type: 'post',
                            url: '{{ url_for("tasks.task-execute", pk=default_pk) }}'.replace(bees.defaultPK, data.id),
                            data : {},
                            success: function (result) {
                                if (result.code != 200) {
                                    bees.tipError({
                                        message: result.data.message
                                    });
                                }else {
                                   bees.tipSuccess({
                                        message: "执行成功"
                                   });
                                   window.location.href = '{{ url_for("tasks.history-list") }}';
                                }
                            }
                        })
                    }
                })
            })

        })
    </script>
{% endblock %}
